Skip to content

fix(client): resolve SSE endpoint URL and origin against redirected response URL - #3241

Open
kingroyale537 wants to merge 2 commits into
modelcontextprotocol:mainfrom
kingroyale537:fix/sse-redirected-endpoint-origin
Open

kingroyale537 wants to merge 2 commits into
modelcontextprotocol:mainfrom
kingroyale537:fix/sse-redirected-endpoint-origin

Conversation

@kingroyale537

Copy link
Copy Markdown

Fixes #3240.

When SSEClientTransport follows a redirect during the GET handshake (e.g. redirectPolicy: 'follow'), this._url still holds the pre-redirect URL, causing relative endpoint events to resolve against the original origin and fail the same-origin check.

Track the final response.url from the EventSource fetch callback and resolve/validate endpoint events against it, falling back to this._url if absent.

@kingroyale537
kingroyale537 requested a review from a team as a code owner November 27, 2026 04:15
@changeset-bot

changeset-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 1d15d0f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@3241

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/@modelcontextprotocol/codemod@3241

@modelcontextprotocol/core

npm i https://pkg.pr.new/@modelcontextprotocol/core@3241

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@3241

@modelcontextprotocol/server-legacy

npm i https://pkg.pr.new/@modelcontextprotocol/server-legacy@3241

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@3241

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@3241

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@3241

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@3241

commit: 1d15d0f

@claude claude Bot added the v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes label Nov 27, 2026
@SyedTashfin

Copy link
Copy Markdown
Contributor

The fix is right for the default policy, but the endpoint origin guard changes its reference under redirectPolicy: 'follow'.

Before, this._endpoint.origin !== this._url.origin pinned the endpoint to the origin the client was configured with. It is now compared against baseUrl.origin, and with 'follow' the stream can land on any origin (the option's own docs say so), so the endpoint may sit on that other origin and receives the POSTs, Authorization from authProvider included.

Reproduced against this branch, with the transport configured at server A: A answers 307 Location: <B>/sse, and B answers the GET with event: endpoint / data: /messages. connect() succeeds and send() posts to B/messages with authorization: Bearer access-1. On main the same setup resolves /messages against A instead.

#3240's author proposed using the final response.url as the base but gating on isWithinOrigin(this._url, final), so 'follow' does not widen what the endpoint may point at. Under the default 'same-origin' policy that gate is a no-op, since fetchWithinOrigin already bounds the hop, so it costs nothing for the bug this PR fixes.

I'm not arguing the wider bound is wrong under 'follow' — a redirected POST already carries its headers to another origin, and there is a test for that. Only that it should be a deliberate choice: nothing in packages/client/test covers a rejected foreign-origin endpoint at all, so this guard has no test on either side today. Happy to send the failing-before case as a test if that helps.

Separately, changeset-bot flagged that the branch carries no changeset.

@Pablo-aps

Copy link
Copy Markdown

I checked the path-resolution part against both released @modelcontextprotocol/client@2.4.0 and the PR build at 3328fa2, using a real local HTTP server and Node 24.11.1. No auth or external server was involved.

All cases started at /old/sse and followed a same-origin 307 with the default redirect policy:

Final GET URL SSE endpoint data POST on 2.4.0 POST on this PR
/new/sse messages?sessionId=test /old/messages?sessionId=test /new/messages?sessionId=test
/new/sse/ messages?sessionId=test /old/messages?sessionId=test /new/sse/messages?sessionId=test
/new/sse /new/messages?sessionId=test /new/messages?sessionId=test /new/messages?sessionId=test

The new test in this PR uses a leading-slash endpoint on another origin under follow, so it exercises the changed origin check. It does not cover the relative-path failure from #3240. I'd add the first two rows under the default policy, keeping the query string assertion. The trailing-slash case catches accidental normalization to the final URL's parent directory. The third row is a useful compatibility control.

This verifies the path behavior only; it does not resolve the separate follow origin-policy question raised in the review above.

@kingroyale537

Copy link
Copy Markdown
Author

Good catch @SyedTashfin and @Pablo-aps — updated in 1d15d0f:

  • Resolve messageEvent.data against responseUrl ?? this._url, while keeping the origin check pinned to this._url.origin so redirectPolicy: 'follow' doesn't widen what endpoint origin is accepted.
  • Added the same-origin redirect relative-path cases (/new/sse and /new/sse/ with messages?sessionId=test, plus /new/messages?sessionId=test) and a foreign-origin rejection test under follow.

@SyedTashfin

Copy link
Copy Markdown
Contributor

One correction to my comment above, because it matters for how the guard is reviewed.

I wrote that "nothing in packages/client/test covers a rejected foreign-origin endpoint at all, so this guard has no test on either side today". The second half of that was wrong: this PR already adds SSE endpoint event resolves relative URL and origin against final redirected URL in client/redirects.test.ts, which redirects the stream to a second origin and asserts the POST lands on that origin's /messages. That is the accepting side of the check, on a different origin, under follow. I had grepped for the error message instead of reading the test diff, so I missed it.

What I should have written: the accepting side is covered by the new test, the rejecting side is not. 1d15d0f fills that gap with SSE endpoint event still rejects foreign-origin endpoints after a cross-origin redirect, so both sides are now covered, along with the relative-path cases.

Thanks for turning this around, and to @Pablo-aps for checking the path behaviour independently.

@kingroyale537

Copy link
Copy Markdown
Author

Thanks both for checking and verifying the redirect/origin cases @SyedTashfin @Pablo-aps!

@Pablo-aps

Copy link
Copy Markdown

I reran the localhost check against the updated 1d15d0f build with the default redirect policy. All three cases pass, including /new/sse/ resolving messages?sessionId=test to /new/sse/messages?sessionId=test. The leading-slash case still posts to /new/messages?sessionId=test. The two relative cases still fail on released 2.4.0.

This rerun covered same-origin path resolution with no auth and no external server.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[v2] SSEClientTransport resolves the endpoint event against the configured URL instead of the URL it was redirected to

3 participants